<?php

//货品列表控制器

class GoodslistController extends AuthController{
	
	//首页
	public function index(){
		$gid=Q('get.gid');
		$tid=Q('get.tid');
		$spec=K('Attr')->field('taid,taname')->where("tid={$tid} AND class=1")->all();
		
		//规格选择
		$db = K('Goodsattr');
		$field = array('gtid', 'gtvalue');
		foreach ($spec as $k => $v)
		{
			$where = array('gid' => $gid, 'taid' => $v['taid']);
			$spec[$k]['opt'] = $db->field($field)->where($where)->all();
			
		}
	    $this->assign('spec',$spec);
		
		//获取list表单内容
		$listData=K('Goodslist')->where("gid={$gid}")->all();
		$da=array();
		if($listData){
			foreach ($listData as $key => $value) {
				$lData=explode(',',$value['combine']);
				$data=array();
				foreach ($lData as $k => $v) {
					$gtvalue=K('Goodsattr')->where("gtid={$v}")->field('gtvalue')->find();
					$data[]=$gtvalue['gtvalue'];
					
				}
				$da[]=array(
				   'combine' =>$data,
				   'glid' =>$value['glid'],
				   'number' =>$value['number'],
				   'inventory' =>$value['inventory'],
				    'gid' =>$value['gid'],
				);
				
			}
			
			$this->assign('da',$da);
		}
		
		if(IS_POST){
			$data=Q('post.');
			
			$data['gid']=$gid;
			$data['combine']=implode(',', $data['spec']);
			K('Goodslist')->add($data);
			$this->success('保存添加成功');
		}
		
		
		$this->display();
	}
	
    public function edit(){
    	$glid=Q('get.glid');
		$gid=Q('get.gid');
		$goods=K('Goods')->where("gid={$gid}")->find();
		$tid=$goods['tid'];
		$spec=K('Attr')->field('taid,taname')->where("tid={$tid} AND class=1")->all();
		//规格选择
		$db = K('Goodsattr');
		$field = array('gtid', 'gtvalue');
		foreach ($spec as $k => $v)
		{
			$where = array('gid' => $gid, 'taid' => $v['taid']);
			$spec[$k]['opt'] = $db->field($field)->where($where)->all();
			
		}
		$this->assign('spec',$spec);
		
		$listData=K('Goodslist')->where("glid={$glid}")->find();
		
		$this->assign('listData',$listData);
		$list=explode(',', $listData['combine']);
		$this->assign('list',$list);
		
		if(IS_POST){
			$data=Q('post.');
			$listdata=array(
			   'glid' =>$glid,
			   'combine' =>implode(',', $data['spec']),
			   'number' =>$data['number'],
			   'inventory' =>$data['inventory'],
			   'gid' => $gid,
			);
			K('Goodslist')->where("glid={$glid}")->update($listdata);
			$this->success('修改成功',U('Admin/Goods/index'));
		}
    	$this->display();
    }
	
	
	//删除规格
	public function del(){
		$glid=Q('get.glid',0,'intval');
		K('Goodslist')->where("glid={$glid}")->delete();
		$this->success('删除成功',U('Admin/Goods/index'));
	}
	
}






?>